package role

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SelectListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleSelectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SelectListLogic {
	return &SelectListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SelectListLogic) RoleSelectList(req *types.RoleSelectListReq) (resp *types.RoleSelectListResp, err error) {
	// 1.builder rpc request
	rpcReq := pb.RoleSelectListReq{RoleName: req.RoleName}
	// 2.request rpc
	selectResp, err := l.svcCtx.UserCenterClient.RoleSelectList(l.ctx, &rpcReq)
	if err != nil {
		return nil, err
	}
	roleArr := selectResp.GetRoleArr()
	var roleRespArr []types.RoleInfoListResp
	if len(roleArr) < 1 {
		return &types.RoleSelectListResp{RoleArr: roleRespArr}, nil
	}
	// 3.return obj
	for _, v := range roleArr {
		roleRespArr = append(roleRespArr, types.RoleInfoListResp{RoleId: v.RoleId, RoleName: v.RoleName, Remark: v.Remark,
			CreateTime: v.CreateTime})
	}
	return &types.RoleSelectListResp{RoleArr: roleRespArr}, nil
}
